package com.agileboot.admin.controller.bot;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.bot.conversationRecord.ConversationRecordApplicationService;
import com.agileboot.domain.bot.conversationRecord.command.AddConversationRecordCommand;
import com.agileboot.domain.bot.conversationRecord.command.UpdateConversationRecordCommand;
import com.agileboot.domain.bot.conversationRecord.dto.ConversationRecordDTO;
import com.agileboot.domain.bot.conversationRecord.query.ConversationRecordQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "会话记录API", description = "会话记录相关的增删查改")
@RestController
@RequestMapping("/bot/conversation-record")
@Validated
@RequiredArgsConstructor
public class ConversationRecordController extends BaseController {

    private final ConversationRecordApplicationService conversationRecordApplicationService;

    @Operation(summary = "会话记录列表")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<ConversationRecordDTO>> list(ConversationRecordQuery query) {
        PageDTO<ConversationRecordDTO> pageDTO = conversationRecordApplicationService.getConversationRecordList(query);
        return ResponseDTO.ok(pageDTO);
    }

    @Operation(summary = "导出会话记录")
    @AccessLog(title = "会话记录", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/excel")
    public void export(HttpServletResponse response, ConversationRecordQuery query) {
        List<ConversationRecordDTO> all = conversationRecordApplicationService.getConversationRecordListAll(query);
        CustomExcelUtil.writeToResponse(all, ConversationRecordDTO.class, response);
    }

    @Operation(summary = "会话记录详情")
    @GetMapping("/{id}")
    public ResponseDTO<ConversationRecordDTO> getInfo(@PathVariable Long id) {
        ConversationRecordDTO dto = conversationRecordApplicationService.getConversationRecordInfo(id);
        return ResponseDTO.ok(dto);
    }

    @Operation(summary = "添加会话记录")
    @AccessLog(title = "会话记录", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddConversationRecordCommand command) {
        conversationRecordApplicationService.addConversationRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改会话记录")
    @AccessLog(title = "会话记录", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateConversationRecordCommand command) {
        conversationRecordApplicationService.updateConversationRecord(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除会话记录")
    @AccessLog(title = "会话记录", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        conversationRecordApplicationService.deleteConversationRecord(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}
